home *** CD-ROM | disk | FTP | other *** search
- /***
- *dirent.c - disk directory maintenence
- *
- *Copyright (c) 1993-1994, Gregg Jennings. All wrongs reserved.
- * P O Box 200, Falmouth, MA 02541-0200
- *
- *Purpose:
- * Handles the displaying and editing of directory sectors for DISKED.C.
- *
- *Notice:
- * This progam may be freely used and distributed. Any distrubution
- * with modifications must retain the above copyright statement and
- * modifications noted.
- * No pulp-publication, in whole or in part, permitted without
- * permission (magazines or books).
- *******************************************************************************/
-
- /*
- Versions:
-
- 1.2 14-Jan-1994 =0 bug fix in changedir()
- 1.1 13-Nov-1993 Started structures
- 1.0 June 1993
-
- Release Notes:
-
- This is all "Brute Force" code. Written in a couple a days after
- I wanted to arange all my directories is a certain non-sorted way
- easier than a touch program.
- */
-
- #include <stdio.h>
- #include <conio.h>
- #include <ctype.h>
-
- #include "mylib.h"
- #include "keys.h"
- #include "dirent.h"
-
- #define isexten(c) ( (c>0x7f) && (c<255) )
-
- static int attrib(unsigned char *b);
- static int text(int len, unsigned char *b);
- static int dostime(unsigned int time, unsigned int *t);
- static int dosdate(unsigned int date, unsigned int *d);
- static int command(int c);
- static int pdirent(unsigned char *buf);
- static void distime(DosTime *dt);
- static void disdate(DosDate *dd);
-
- /* ...filename.txt...ttri...hh:mm:ss...mm/dd/yy...1234567...1234567 */
- static char
- *dir_header=" File Attr Time Date Cluster Size\n";
-
- /* display directory */
-
- extern void dumpdir(unsigned char *buf, int sec_size)
- {
- int i;
-
- send('\n');
- send('\n');
- print(dir_header); /* print header */
-
- for (i = 0; i < sec_size; i+=32) /* and each entry */
- {
- send('\n');
- put(3,' ');
- pdirent(buf+i);
- }
- send('\n');
- }
-
- /* change dir command structure */
-
- /* ...filename.txt...ttri...hh:mm:ss...mm/dd/yy...1234567...1234567 */
- struct fields {
- int beg; /* starting column */
- int end; /* ending column */
- int (*inp)(); /* function to do the change */
- } field[] = {
- 4,7, command,
- 9,16, text,
- 18,20,text,
- 24,28,attrib,
- 32,33,dostime,
- 43,44,dosdate,
- };
-
- /*
- NOTE: The above structure definition uses int (*inp)(); to define
- a pointer to a function. Because different functions are called,
- each taking different arguments, the arguments in the definition
- are left blank. This works fine except for the first call to one
- instance will result in the warning:
-
- 'field' : no function prototype given
-
- I dont know how to get around this.
- */
-
- /***
- *changedir() - edit directory entry(s) in sector buffer
- *
- * ver 1.1 fixed =0 bug
- ****/
-
- extern int changedir(unsigned char *buf,int sec_size)
- {
- int i,r,o;
- byte a,x;
- unsigned int *b;
- unsigned int c;
- unsigned int n;
- int p; /* absolute position */
- int f; /* field in */
-
- getcursor(&a,&x);
- send('\n');
- send('\n');
- put(6,' ');
- print(dir_header);
- i = r = 0;
- for (;;)
- {
- f = 0; /* start off at command function */
- p=field[f].beg;
- send('\n');
- pnlz(i+1,3,10);
- put(6,' ');
- o=i*32;
- r=pdirent(buf+o);
- get:
- setcursor(a,(byte)p);
-
- switch (f)
- {
- case 0: /* command */
- c = (*field[f].inp)(r);
- break;
- case 1: /* file name */
- c = (*field[f].inp)(8,buf+o);
- break;
- case 2: /* file extension */
- c = (*field[f].inp)(3,buf+o+8);
- break;
- case 3: /* attribute */
- c = (*field[f].inp)(buf+o+11);
- break;
- case 4: /* time */
- b=(unsigned int *)(buf+o+0x16);
- c = (*field[f].inp)(*b,b);
- break;
- case 5: /* date */
- b=(unsigned int *)(buf+o+0x18);
- c = (*field[f].inp)(*b,b);
- break;
- default:
- break;
- }
-
- /* deal with key returned from functions */
-
- if (c==RIGHT)
- c='\t';
- else if (c==SHTAB || c==CLEFT)
- c=0x7f;
- else if (c==LEFT) /* goto previous sub-field */
- {
- if (f > 0) /* if not in command field */
- if (p > field[f].beg)
- --p;
- else /* if at first sub-field */
- p=field[--f].beg; /* goto previous field */
- }
- else if (c==CRIGHT)
- c='\t';
- else if (c==DOWN)
- c='\r';
- else if (c==UP)
- c='\b';
- c&=0xff;
- if (c==0x7f) /* goto previoud field */
- {
- if (f>0)
- p=field[--f].beg;
- }
- if (c=='\t') /* goto next field */
- {
- if (f<sizeof(field)/sizeof(struct fields)-1)
- p=field[++f].beg;
- }
- if (c=='\r' || c==' ') /* goto next entry */
- {
- if (i==(sec_size/32)-1)
- {
- send('\n');
- send('\n');
- put(6,' ');
- print(dir_header);
- i=0;
- }
- else
- i++;
- continue;
- }
- if (c=='\b') /* goto previous entry */
- {
- if (i)
- i--;
- else
- {
- i=(sec_size/32)-1;
- send('\n');
- send('\n');
- put(6,' ');
- print(dir_header);
- }
- continue;
- }
- if (c=='=') /* goto selected entry */
- {
- conout(c);
- if (getnum(sec_size/32,&n,10) > 0 && n > 0)
- i = n-1;
- continue;
- }
- if (c=='/' || c=='?')
- {
- print("\n\n\tCR/SP next file");
- print("\n\tBS previous file");
- print("\n\tTAB next field");
- print("\n\tSHTAB previous field");
- print("\n\t=n goto file n");
- print("\n\t.|ESC exit\n");
- continue;
- }
- if (c=='.' || c==ESC)
- break;
- goto get;
- }
- send('\n');
- return(0);
- }
-
- static int command(int c)
- {
- int i;
-
- i=conin();
- if (c<1 && (i==RIGHT || i==TAB))
- i=0;
- return(i);
- }
-
- /***
- *text() -- edit a text entry (filename/ext)
- *
- * can enter ALL printable characters including '.' and ' '
- ****/
-
- static int text(int len, unsigned char *buf)
- {
- int c;
- int i;
-
- for (i=0;;)
- {
- c=conin();
- if (c==RIGHT)
- {
- if (i==len-1)
- break;
- curright();
- ++i;
- continue;
- }
- if (c==LEFT || c==BACKSP)
- {
- if (!i)
- break;
- --i;
- curleft();
- continue;
- }
- if (c==RETURN || c==TAB || c==SHTAB || c==DOWN || c==UP)
- break;
- if (c==ESCAPE)
- return(0);
- if (c>0x352f) continue;
- c&=0xff;
- if (isgraph(c) || isexten(c))
- {
- *(buf+i)=(char)c;
- i++;
- conout(c);
- if (i==len)
- {
- c=RIGHT;
- break;
- }
- }
- }
- return(c);
- }
-
- /***
- *attrib() - toggle filename attributes, except for dir
- *
- ****/
-
- static int attrib(unsigned char *buf)
- {
- int i;
- int b,c,p,d;
- byte isvol;
-
- /* 01234 */
- /* RHSDA */
-
- isvol = (byte)(*buf&8);
- p=0;
-
- for (;;)
- {
- i=conin();
- if (i==RETURN || i==TAB || i==SHTAB || i==DOWN || i==UP)
- break;
- if (i==ESCAPE)
- {
- i=0;
- break;
- }
- if (i==RIGHT)
- {
- if (p==4) /* end */
- break;
- curright();
- ++p;
- continue;
- }
- if (i==LEFT || i==BACKSP)
- {
- if (p==0)
- break;
- --p;
- curleft();
- continue;
- }
-
- if ( !isvol && p!=3 && (i&0xff)==' ')
- {
- if (p==0)
- {
- b=1;
- d='R';
- }
- else if (p==1)
- {
- b=2;
- d='H';
- }
- else if (p==2)
- {
- b=4;
- d='S';
- }
- else if (p==4)
- {
- b=0x20;
- d='A';
- }
- if (*buf&b)
- {
- c='_';
- *buf&=~b;
- }
- else
- {
- c=d;
- *buf|=b;
- }
- conout(c);
- curleft();
- }
- }
- return(i);
- }
-
- static int dostime(unsigned int time, unsigned int *t)
- {
- int i;
- int h,m,s;
- byte r,x;
- int c,p;
-
- /* 01234567 */
- /* hh:mm:ss */
-
- getcursor(&r,&x);
- c=x;
- p=c;
- h=time>>11;
- m=(time>>5)&0x3f;
- s=time&0x1f;
-
- for (;;)
- {
- setcursor(r,(byte)p);
- i=conin();
-
- if (i==TAB || i==RETURN || i==SHTAB || i==DOWN || i==UP)
- return(i);
- if (i==ESCAPE)
- {
- i=0;
- break;
- }
-
- if (i==LEFT || i==BACKSP)
- {
- if (p==c && i==LEFT)
- return(i);
- if (p==c && i==BACKSP)
- continue;
- if (p==c+3 || p==c+6)
- p-=2;
- else
- p--;
- continue;
- }
- right:
- if (i==RIGHT)
- {
- if (p==c+7)
- return(i);
- if (p==c+1 || p==c+4)
- p+=2;
- else
- p++;
- continue;
- }
- i&=0xff;
- if (isdigit(i))
- {
- conout(i);
- i=i-'0';
- if (p==c)
- h=(i*10)+(h%10);
- else if (p==c+1)
- h=i+((h/10)*10);
- else if (p==c+3)
- m=(i*10)+(m%10);
- else if (p==c+4)
- m=i+((m/10)*10);
- else if (p==c+6)
- s=(i*10)+(s%10);
- else if (p==c+7)
- s=i+((s/10)*10);
- time=(h<<11)+(m<<5)+s;
- *t=time;
- i=RIGHT;
- goto right;
- }
- }
- return(i);
- }
-
- /***
- *dosdate() - edit date field
- *
- * ver 1.1 fixed right arrow at last number from wrapping
- ****/
-
- static int dosdate(unsigned int date, unsigned int *buf)
- {
- int i;
- int m,d,y;
- byte r,x;
- int c,p;
-
- /* 01234567 */
- /* mm/dd/yy */
-
- getcursor(&r,&x);
- c=x;
- p=c;
- m=(date>>5)&0xf;
- d=date&0x1f;
- y=((date>>9)&0x7f)+80;
-
- for (;;)
- {
- setcursor(r,(byte)p);
- i=conin();
-
- if (i==TAB || i==RETURN || i==SHTAB || i==DOWN || i==UP)
- break;
- if (i==ESCAPE)
- return(0);
- if (i==LEFT || i==BACKSP)
- {
- if (p==c && i==LEFT)
- return(i);
- if (p==c && i==BACKSP)
- continue;
- if (p==c+3 || p==c+6)
- p-=2;
- else
- p--;
- continue;
- }
- right:
- if (i==RIGHT)
- {
- if (p==c+7)
- continue;
- if (p==c+1 || p==c+4)
- p+=2;
- else
- p++;
- continue;
- }
- i&=0xff;
- if (isdigit(i))
- {
- conout(i);
- i=i-'0';
- if (p==c)
- m=(i*10)+(m%10);
- else if (p==c+1)
- m=i+((m/10)*10);
- else if (p==c+3)
- d=(i*10)+(d%10);
- else if (p==c+4)
- d=i+((d/10)*10);
- else if (p==c+6)
- y=(i*10)+(y%10);
- else if (p==c+7)
- y=i+((y/10)*10);
- if (y>80)
- y-=80;
- else
- y=0;
- date=(y<<9)+(m<<5)+d;
- y+=80;
- *buf=date;
- i=RIGHT;
- goto right;
- }
- }
- return(i);
- }
-
- /***
- *pdirent() - print directory entry
- *
- * v1.1 removed superfluous pointer
- ****/
-
- static int pdirent(unsigned char *buf)
- {
- int i,r;
- long *l;
- unsigned char c;
- unsigned char *a;
- unsigned int m;
- char *attr = "RHSvDA";
-
- r = 1;
- a = buf+0xb;
- if (*(buf) == 0)
- {
- print(" unused");
- r = 0;
- }
- else
- {
- if (*(buf) == 0xE5)
- r = -1;
- for (i = 0; i < 8; i++)
- {
- c = *(buf+i);
- if (isspace((int)c) || c==255 || !c || c==7 || c==8)
- conout(' ');
- else
- conout(c);
- }
- conout('.');
- for (; i < 11; i++)
- {
- c = *(buf+i);
- if (isspace((int)c) || c==255 || !c || c==7 || c==8)
- conout(' ');
- else
- conout(c);
- }
- put(3,' ');
- if (*a&8)
- print(" Vol ");
- else
- {
- for (m = 1,i = 0; i < 6; m<<=1,i++)
- {
- if (i==3) /* skip vol */
- continue;
- if (*a&m)
- conout(*(attr+i));
- else
- conout('_');
- }
- }
- put(3,' ');
- distime((DosTime *)(buf+0x16));
- put(3,' ');
- disdate((DosDate *)(buf+0x18));
- put(3,' ');
- pnls(*(unsigned int *)(buf+0x1a),7,10);
- if (!(*a&0x10))
- {
- put(3,' ');
- l = (long *)(buf+0x1c);
- plnls(*l,7,10);
- }
- }
- return(r);
- }
-
- static void distime(DosTime *dt)
- {
- pnlz(dt->hours,2,10);
- conout(':');
- pnlz(dt->minutes,2,10);
- conout(':');
- pnlz(dt->seconds,2,10);
- }
-
- static void disdate(DosDate *dd)
- {
- pnlz(dd->month,2,10);
- conout('/');
- pnlz(dd->day,2,10);
- conout('/');
- pnlz(dd->year+80,2,10);
- }
-